package cc.wanforme.fsync.server.task;

import jakarta.websocket.Session;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class TaskWebsocketSessions {

    final Map<Long, Vector<Session>> sessions = new ConcurrentHashMap<>();

    /** 添加用户 session */
    public void add(Long userId, Session session) {
        Vector<Session> list = sessions.computeIfAbsent(userId, e -> new Vector<>(1));
        list.add(session);
    }

    /** 删除用户 session */
    public void remove(Long userId, Session session) {
        Vector<Session> list = sessions.get(userId);
        if(list==null || list.isEmpty()) {
            return;
        }
        list.remove(session);
        if(list.isEmpty()) {
            sessions.remove(userId);
        }
    }

    /**
     * 获取用户 session
     *
     * @return
     */
    public Vector<Session> list(Long userId) {
        return sessions.get(userId);
    }

}
